package org.example.likou150.circularList;

import java.util.ArrayList;

// https://leetcode.cn/problems/rotate-list/?envType=study-plan-v2&envId=top-interview-150
public class RotateRight {


    public ListNode rotateRight(ListNode head, int k) {
        ArrayList<ListNode> nodeList = new ArrayList<>();
        ListNode tempNode = head;
        while (tempNode != null){
            nodeList.add(tempNode);
            tempNode = tempNode.next;
        }
        if(nodeList.isEmpty() || nodeList.size()==1) return head;
        k = k % nodeList.size();
        if(k == 0) return head;
        nodeList.get(nodeList.size()-1).next = nodeList.get(0);
        nodeList.get(nodeList.size()-k-1).next = null;
        return nodeList.get(nodeList.size()-k);
    }

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}
